package com.power.dao.impl;

import com.power.dao.InspeDao;
import com.power.ov.ResultDataTable;
import com.power.pojo.Defect;
import com.power.pojo.Inspetion;
import com.power.pojo.Users;
import com.power.utils.JdbcBuild;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 李辽
 * Date: 2020/10/09 11:02
 * Description:
 * Version: V1.0
 */
public class InspeDaoimpl extends JdbcBuild implements InspeDao {
    @Override
    public ResultDataTable QueryListInspection(int page, int limit) throws SQLException {
        String sql ="SELECT * FROM inspection AS i ,yesno AS Y,states AS s,line AS l,users AS u WHERE i.line_id=l.line_number AND i.is_detele=y.yesno_id AND i.task_state=s.state_id AND u.user_account=i.lineman_account limit ?,?";
        int param1=(page-1)*limit;
        int param2=limit;
        Object[] params = {param1,param2};
        List<Map<String, Object>> inspetions = super.findAllForList(sql, params);
        ResultDataTable resultDataTable = new ResultDataTable();
        resultDataTable.setData(inspetions);
        resultDataTable.setCount(queryCountInspection());
        resultDataTable.setCode(0);
        resultDataTable.setMessage("查询成功");
        System.out.println("到达dao层,数据为："+resultDataTable);
        return resultDataTable;
    }

    private long queryCountInspection() throws SQLException {
        String sql ="SELECT count(*) FROM inspection AS i ,yesno AS Y,states AS s,line AS l,users AS u WHERE i.line_id=l.line_number AND i.is_detele=y.yesno_id AND i.task_state=s.state_id AND u.user_account=i.lineman_account;\n";
        int allForInt = super.findAllForInt(sql, null);
        return allForInt;
    }

    @Override
    public int AddInspetion(Inspetion inspetion) throws SQLException {
        String sql ="INSERT INTO inspection(task_id,task_name,line_id,inspection_account,lineman_account,lssue_time,task_des) VALUES (?,?,?,?,?,?,?)";
        Object[] params={inspetion.getTask_id(),inspetion.getTask_name(),inspetion.getLine_id(),inspetion.getInspection_accont(),inspetion.getLineman_account(),inspetion.getLssue_time(),inspetion.getTask_des()};
        int result = super.executeUpdate(sql, params);
        System.out.println("到达后代dao层，结果为："+result);
        return result;
    }

    @Override
    public int queryLinaNameByLineId(Inspetion inspetion) throws SQLException {
        String sql = "select line_id from line where line_name=?";
        Inspetion result = super.queryEntity(Inspetion.class, sql, inspetion.getLine_name());
        return result.getLine_id();
    }

    @Override
    public String queryUserAcountByInspetionAcount(String inspection_accont) throws SQLException {
        String sql = "select user_account from users where user_name=?";
        Users users = super.queryEntity(Users.class, sql, inspection_accont);
        return users.getUser_account();
    }

    @Override
    public ResultDataTable ShowSelectInspection() throws SQLException {
        String sql = "SELECT * FROM inspection";
        List<Map<String, Object>> inspetions = super.findAllForList( sql, null);
        ResultDataTable resultDataTable = new ResultDataTable();
        resultDataTable.setData(inspetions);
        resultDataTable.setCode(0);
        resultDataTable.setMessage("查询成功");
        return resultDataTable;
    }

    @Override
    public ResultDataTable ShowSelectState() throws SQLException {
        String sql = "SELECT * FROM states";
        List<Map<String, Object>>inspetions = super.findAllForList(sql, null);
        ResultDataTable resultDataTable = new ResultDataTable();
        resultDataTable.setData(inspetions);
        resultDataTable.setCode(0);
        resultDataTable.setMessage("查询成功");
        return resultDataTable;
    }

    @Override
    public ResultDataTable SearchInspection(String task_id, String line_number, int state_id) throws SQLException {
        String sql ="SELECT * FROM inspection AS i ,yesno AS Y,states AS s,line AS l,users AS u WHERE i.line_id=l.line_number AND i.is_detele=y.yesno_id AND i.task_state=s.state_id AND u.user_account=i.lineman_account";
        List list = new ArrayList();
        if(task_id != null && !"".equals(task_id)){
            sql+=" and task_id=?";
            list.add(task_id);
        }
        if(line_number != null && !"".equals(line_number)){
            sql+=" and l.line_number=?";
            list.add(line_number);
        }
        if(state_id > 0){
            sql+=" and state_id=?";
            list.add(state_id);
        }
        System.out.println(sql);
        Object[] params =list.toArray();
        List<Map<String, Object>> inspetions = super.findAllForList( sql, params);

        ResultDataTable resultDataTable = new ResultDataTable();
        resultDataTable.setData(inspetions);
        resultDataTable.setCode(0);
        resultDataTable.setMessage("查询成功");

        return resultDataTable;
    }

    @Override
    public int EditInspetion(Inspetion inspetion) throws SQLException {
        String sql = "UPDATE inspection SET task_name=?,line_id=?,inspection_account=?,task_des=? WHERE task_id = ?";
        Object[] params = {inspetion.getTask_name(),inspetion.getLine_name(),inspetion.getInspection_accont(),inspetion.getTask_des(),inspetion.getTask_id()};
        int result = super.executeUpdate(sql,params);
        System.out.println("后台dao中的result数据"+result);
        return result;
    }

    @Override
    public int ChangeCarrieState(String task_id) throws SQLException {
        String sql = "UPDATE inspection SET task_state = 6 WHERE task_id=?";
        int result = super.executeUpdate(sql, task_id);
        return result;
    }

    @Override
    public String setUserCount(Defect defect) throws SQLException {
        String sql = "select * from users where user_name = ?";
        Users users = super.queryEntity(Users.class, sql, defect.getUser_name());
        return users.getUser_account();
    }

    @Override
    public int Edit_CarriedInspe(Defect defect) throws SQLException {
        String sql = "INSERT INTO defect_details(defect_id,line_id,tower_id,defect_grade,inspector_account,intact,find_time,defect_des) VALUES (?,?,?,?,?,?,?,?)";
        Object[] params={defect.getDefect_id(),defect.getLine_id(),defect.getTower_id(),defect.getDefect_grade(),defect.getInspector_account(),defect.getIntact(),defect.getFind_time(),defect.getDefect_des()};
        int result = super.executeUpdate(sql, params);
        return result;
    }
}
